package com.yiren.dbaa.module.device.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.LinkedList;
import java.util.List;

/**
 * 自研机械档位描点
 */
public class ZiyanGyroPointView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder holder;
    private Paint paintGrid;  //网格线画笔
    private Paint paintLine;  //绘制线
    private Paint paintPoint1;//绘制最小的点
    private Paint paintPoint2;//绘制最大的点

    private int width, height;
    private float max, scale;
    private boolean running;

    private List<Float> xPoints1;  //最小的X集合
    private List<Float> yPoints1;  //最小的Y集合
    private List<Float> xPoints2;  //最大的X集合
    private List<Float> yPoints2;  //最大的Y集合

    private List<Float> xPoints;   //实时移动x坐标
    private List<Float> yPoints;   //实时移动x坐标

    public ZiyanGyroPointView(Context context) {
        this(context, null);
    }

    public ZiyanGyroPointView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZiyanGyroPointView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paintGrid = new Paint();
        paintGrid.setStyle(Paint.Style.FILL);
        paintGrid.setColor(0xFFAAAAAA);
        paintGrid.setStrokeWidth(1);
        paintGrid.setPathEffect(new DashPathEffect(new float[]{8, 8}, 0));

        paintLine = new Paint();
        paintLine.setStyle(Paint.Style.FILL);
        paintLine.setAntiAlias(true);
        paintLine.setStrokeWidth(2);

        paintPoint1 = new Paint();
        paintPoint1.setStyle(Paint.Style.FILL);
        paintPoint1.setAntiAlias(true);
        paintPoint1.setColor(0xFF33CC33);

        paintPoint2 = new Paint();
        paintPoint2.setStyle(Paint.Style.FILL);
        paintPoint2.setAntiAlias(true);
        paintPoint2.setColor(0xFF3333CC);

        max = 60;
        scale = 1;
        running = false;

        xPoints1 = new LinkedList<>();
        yPoints1 = new LinkedList<>();
        xPoints2 = new LinkedList<>();
        yPoints2 = new LinkedList<>();
        for (int i = 0; i < 6; i++) {
            xPoints1.add(0f);
            yPoints1.add(0f);
            xPoints2.add(0f);
            yPoints2.add(0f);
        }

        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        this.holder = holder;
        (new Thread(this)).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        this.width = width;
        this.height = height;

        int len = Math.min(width, height);
        scale = len / max;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        running = false;
    }

    @Override
    public void run() {
        System.out.println("GYROPointView start.");
        running = true;

        while (running) {
            if (holder != null) {
                Canvas canvas = holder.lockCanvas();
                if (null != canvas) {
                    canvas.drawColor(0xFFFFFFFF);
                    drawGrid(canvas);
                    drawPoints(canvas);
                    drawPoints1(canvas);
                    drawPoints2(canvas);
                    drawStudy1Rect(canvas);
                    holder.unlockCanvasAndPost(canvas);
                }
            }
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        running = false;
        System.out.println("GYROPointView finish.");
    }

    /**
     * 绘制网格线
     */
    private void drawGrid(Canvas canvas) {
        for (int i = 0; i * 10 <= max; i++) {
            float g = toPixel(i * 10);
            canvas.drawLine(0, height / 2.0f - g, width, height / 2.0f - g, paintGrid);
            canvas.drawLine(0, height / 2.0f + g, width, height / 2.0f + g, paintGrid);
            canvas.drawLine(width / 2.0f - g, 0, width / 2.0f - g, height, paintGrid);
            canvas.drawLine(width / 2.0f + g, 0, width / 2.0f + g, height, paintGrid);
        }
    }

    private void drawPoints(Canvas canvas) {
        if (null != xPoints && null != yPoints) {
            for (int i = 0; i < xPoints.size(); i++) {
                if (i < xPoints.size() && i < yPoints.size()) {
                    if (null != xPoints.get(i) && null != yPoints.get(i)) {
                        float x = xPoints.get(i);
                        float y = yPoints.get(i);
                        canvas.drawCircle(width / 2.0f + toPixel(x), height / 2.0f - toPixel(y), 4, paintPoint1);
                    }
                }
            }
        }
    }

    private synchronized void drawPoints1(Canvas canvas) {
        for (int i = 0; i < xPoints1.size(); i++) {
            float x = xPoints1.get(i);
            float y = yPoints1.get(i);
            canvas.drawCircle(width / 2.0f + toPixel(x), height / 2.0f - toPixel(y), 4, paintPoint1);
        }
    }

    private synchronized void drawPoints2(Canvas canvas) {
        for (int i = 0; i < xPoints2.size(); i++) {
            float x = xPoints2.get(i);
            float y = yPoints2.get(i);
            canvas.drawCircle(width / 2.0f + toPixel(x), height / 2.0f - toPixel(y), 4, paintPoint2);
        }
    }

    private synchronized void drawStudy1Rect(Canvas canvas) {
        paintLine.setColor(0xFFCC33CC);
        for (int i = 0; i < xPoints1.size(); i++) {
            canvas.drawLine(width / 2.0f + toPixel(xPoints1.get(i)), height / 2.0f - toPixel(yPoints2.get(i)),
                    width / 2.0f + toPixel(xPoints2.get(i)), height / 2.0f - toPixel(yPoints2.get(i)), paintLine);
            canvas.drawLine(width / 2.0f + toPixel(xPoints2.get(i)), height / 2.0f - toPixel(yPoints2.get(i)),
                    width / 2.0f + toPixel(xPoints2.get(i)), height / 2.0f - toPixel(yPoints1.get(i)), paintLine);
            canvas.drawLine(width / 2.0f + toPixel(xPoints2.get(i)), height / 2.0f - toPixel(yPoints1.get(i)),
                    width / 2.0f + toPixel(xPoints1.get(i)), height / 2.0f - toPixel(yPoints1.get(i)), paintLine);
            canvas.drawLine(width / 2.0f + toPixel(xPoints1.get(i)), height / 2.0f - toPixel(yPoints1.get(i)),
                    width / 2.0f + toPixel(xPoints1.get(i)), height / 2.0f - toPixel(yPoints2.get(i)), paintLine);
        }
    }

    public void setMax(int max) {
        this.max = max;
        int len = Math.min(width, height);
        scale = len / this.max;
    }

    public void setStudy1Rect(int index, float minX, float maxX, float minY, float maxY) {
        if (0 <= index && index < 6) {
            xPoints1.set(index, minX);
            xPoints2.set(index, maxX);
            yPoints1.set(index, minY);
            yPoints2.set(index, maxY);
        }
    }

    public void clearLocations() {
        xPoints1.clear();
        yPoints1.clear();
        xPoints2.clear();
        yPoints2.clear();
    }

    private int toPixel(float degree) {
        return (int) (degree * scale);
    }

    public void setxPoints(List<Float> xPoints) {
        this.xPoints = xPoints;
    }

    public void setyPoints(List<Float> yPoints) {
        this.yPoints = yPoints;
    }
}
